package com.training1.core.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.global.signIn.BOXLoginInfoUtil;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.common.utils.http.IPUtil;
import com.training1.core.entity.RoleEntity;
import com.training1.core.entity.UserRoleEntity;
import com.training1.core.modules.service.BOXLoginService;
import com.training1.core.modules.service.BOXUserEntityService;
import com.training1.core.modules.service.ContractBaseService;
import com.training1.core.modules.service.RoleService;
import com.training1.core.modules.service.UserRoleService;


@Controller
@RequestMapping("Boxuser")
public class BOXUserController extends BaseController{

	@Resource
	private BOXLoginService bloginService;
	@Resource
	private BOXUserEntityService buserService;
	@Resource
	private RoleService roleService;
	@Resource
	private UserRoleService urService;
	@Resource
	private ContractBaseService contractBaseService;
	
	@RequestMapping("boxuseradd")
	public String goAdd(){
		Integer userid = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", 26);
		map.put("userId", userid.toString());
		if(bloginService.getUserRole(map) != 0){
			return "BoxUser/BoxUserAdd";
		}else{
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping("boxuserdelete")
	public String goDelete(Integer id,Model modul){
		modul.addAttribute("id", id);
		
		Integer userid = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", 27);
		map.put("userId", userid.toString());
		if(bloginService.getUserRole(map) != 0){
			return "BoxUser/BoxUserDelete";
		}else{
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping("boxuseredit")
	public String goEdit(Integer id,Model modul){
		modul.addAttribute("editid", id);
		
		Integer userid = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", 28);
		map.put("userId", userid.toString());
		if(bloginService.getUserRole(map) != 0){
			return "BoxUser/BoxUserEdit";
		}else{
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping("boxusersearch")
	public String goSearch(){
		Integer userid = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", 29);
		map.put("userId", userid.toString());
		if(bloginService.getUserRole(map) != 0){
			return "BoxUser/BoxUserSearch";
		}else{
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping("boxuserinfo")
	public String goInfo(String userId,Model modul){
		modul.addAttribute("userId", userId);
		
		Integer userid = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", 30);
		map.put("userId", userid.toString());
		if(bloginService.getUserRole(map) != 0){
			return "BoxUser/BoxUserInfo";
		}else{
			return "contract/notLimitsPage";
		}
	}
	
/*
 * 角色下拉单
 */
	@RequestMapping("userfindName")
	@ResponseBody
	public List<RoleEntity> userfindName(){
		return roleService.userfindName();
	}
	
	/*
	 * 判断用户是否存在
	 */
	@RequestMapping("findUserByUserId")
	@ResponseBody
	public BOXUserEntity findUserByUserId(String userId){
		Map<String,Object> map = new HashMap<>();
		map.put("userId", userId);
		return buserService.findUser(map);
	}
	/*
	 * 保存到用户表
	 */
	@RequestMapping("savetoUser")
	@ResponseBody
	public RespJson savetoUser(BOXUserEntity boxuserEntity){
		boxuserEntity.setModifyCount(0);
		buserService.save(boxuserEntity);
		return RespJsonFactory.buildSuccess();
	}
	/*
	 * 保存到用户角色表
	 */
	@RequestMapping("savetoUserRole")
	@ResponseBody
	public RespJson savetoUserRole(String userId,String roleId){
		Map<String,Object> map = new HashMap<>();
		map.put("userId", userId);
		BOXUserEntity buser = buserService.findUser(map);
		UserRoleEntity urEntity = new UserRoleEntity();
		urEntity.setRoleId(Integer.valueOf(roleId));
		urEntity.setUserId(buser.getId());
		urEntity.setModifyCount(buser.getModifyCount()+1);
		urEntity.setModifyCount(0);
		urService.save(urEntity);
		return RespJsonFactory.buildSuccess();
	}
	/*
	 * 更新到用户表
	 */
	@RequestMapping("updatetoUser")
	@ResponseBody
	public RespJson updatetoUser(BOXUserEntity boxuserEntity){
		buserService.save(boxuserEntity);
		return RespJsonFactory.buildSuccess();
	}
	/*
	 * 用户角色表更新
	 */
	@RequestMapping("updatetoUserRole")
	@ResponseBody
	public RespJson updatetoUserRole(String userId,String roleId){
		UserRoleEntity urEntity = urService.get(Integer.valueOf(userId));
		int c = urEntity.getModifyCount();
		
		urEntity.setRoleId(Integer.valueOf(roleId));
		urEntity.setUserId(Integer.valueOf(userId));
		urEntity.setModifyCount(c+1);
		
		urService.updateByUserId(urEntity);
		return RespJsonFactory.buildSuccess();
	}
	/*
	 * 用户查询
	 */
	@RequestMapping("findlist")
	@ResponseBody
	public Page<BOXUserEntity> findlist(String userId,String userName,String underCompany,Page<BOXUserEntity> page){
		Integer id = BOXLoginInfoUtil.getLoginInfo().getLoginId();
		
		page.getSearch().put("id", id);
		page.getSearch().put("userId", userId);
		page.getSearch().put("userName", userName);
		page.getSearch().put("underCompany", underCompany);
		buserService.findList(page.getSearch());
		return page;
	}
	
	/*
	 * 根据id获取用户
	 */
	@RequestMapping("get")
	@ResponseBody
	public BOXUserEntity get(Integer id){
		BOXUserEntity user = buserService.edit(id);
		return user;
	}
	/*
	 * 根据id获取做成的合同数
	 */
	@RequestMapping("updateById")
	@ResponseBody
	public int updateById(String userId){
		return contractBaseService.countUserId(userId);
	}
	
	/*
	 * 根据id删除用户
	 */
	@RequestMapping("delete")
	@ResponseBody
	public RespJson delete(Integer id,String modifyCount){
		Map<String,Object> map = new HashMap<>();
		map.put("id", id);
		map.put("modifyCount", modifyCount);
		buserService.deleteById(map);
		return RespJsonFactory.buildSuccess();
	}
	
}
